package com.android.multiplesmart.utils;

import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Calendar;
import java.util.Map;

public class HandlersUtils {
    private static HandlersUtils INSTANCE = new HandlersUtils();
    private Context mContext;

    public void init(Context context) {
        Log.e("CrashHandler","...........................2");
        mContext = context;

    }

    /**
     * 保证只有一个CrashHandler实例
     */
    private HandlersUtils() {
    }

    /**
     * 获取CrashHandler实例 ,单例模式
     */
    public static HandlersUtils getInstance() {
        return INSTANCE;
    }
    /**
     * 保存错误信息到文件中
     *
     * @param ex
     * @return 返回文件名称, 便于将文件传送到服务器
     */
    public String saveCrashInfo2File(String message) {
//        Log.e(TAG,"------1"+ ex.getMessage());
        StringBuffer sb = new StringBuffer();
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        String time = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
//        sb.append("\r\n");
        sb.append("\r\n");
        sb.append("\r\n");
        sb.append("************************************************"+time+"****************************************"+"\r\n");
        sb.append("\r\n");
//        for (Map.Entry<String, String> entry : infos.entrySet()) {
//            String key = entry.getKey();
//            String value = entry.getValue();
//            sb.append(key + "=" + value + "\n");
//        }

//        Writer writer = new StringWriter();
//        PrintWriter printWriter = new PrintWriter(writer);
//        ex.printStackTrace(printWriter);
//        Throwable cause = ex.getCause();
//        while (cause != null) {
//            cause.printStackTrace(printWriter);
//            cause = cause.getCause();
//        }
//        printWriter.close();
        String result = message;
        sb.append(result);
        try {
            String fileName = "message.txt";
            if (Build.VERSION.SDK_INT >= 29) {
                File file=mContext.getExternalFilesDir("text");
                File  file2 = new File(file, "message.txt");
                System.out.println(file2.getAbsolutePath()+"获取路是否成功");
                DataUtils.saveAction(file2,sb.toString());
            }else {
                if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                    File dir = mContext.getExternalFilesDir("message");
                    if (!dir.exists()) {
                        dir.mkdir();
                    }
                    File file = new File(dir, fileName);
                    if (!file.exists()) {
                        file.createNewFile();
                    }
                    FileWriter fileWriter = new FileWriter(file, true);
                    fileWriter.write(sb.toString());
                    fileWriter.close();
                }
            }

            return fileName;
        } catch (Exception e) {
            return null;
        }
    }

}
